<?php
	require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'class.api.php');

	class API_CATEGORY extends API
	{
		// {{{ Class variables
		var $fields = array (
			'categoryid',
			'name',
			'description',
			'parentid',
			'icon',
			'security',
			'approvaltype',
			'catorder'
		);

		var $pk = 'categoryid';
		var $categoryid = 0;
		var $name = '';
		var $description = '';
		var $parentid = 0;
		var $pass = '';
		var $icon = '';
		var $security = 'public';
		var $approvaltype = 0;
		var $catorder = 0;


		// }}}

		// {{{ setupDatabase()
		/**
		* Setup the connection to the database and some other database
		* properties
		*
		* @return void
		*/
		function setupDatabase()
		{
			$this->db = $GLOBALS['AKB_DB'];
			$tableSuffix = 'categories';
			$this->table = $GLOBALS['tablePrefix'].$tableSuffix;
			$this->tablePrefix = $GLOBALS['tablePrefix'];
		}
		// }}}

		/**
		* Create a new item in the database
		*
		* @return mixed false if failed to create, the id of the item otherwise
		*/
		function create()
		{
			if (!$this->CategoryExists($_POST['parentid'], $_POST['name'])) {
				return parent::create();
			} else {
				$this->error = GetLang('apiCatAlreadyExists');
				return false;
			}
		}

		/**
		* delete
		* Delete a category, if $id is given and is positive then delete it and
		* delete any category associations it may have
		*
		* @param int $id The id of the category to delete
		*
		* @return bool Was the delete successful ?
		*/
		function delete($id=0)
		{
			if (!parent::delete()) {
				return false;
			}

			// Delete the associations in the categoryassociations table
			$query = "DELETE FROM `".$this->tablePrefix."categoryassociations`
			WHERE `categoryid` IN (".((int) $id).")";
			$this->db->Query($query);

			// Delete the associations in the catassoc_history table
			$query = "DELETE FROM `".$this->tablePrefix."catassoc_history`
			WHERE `categoryid` IN (".((int) $id).")";
			$this->db->Query($query);

			return true;
		}

		/**
		* Delete multiple categories in one database query, useful for bulk
		* actions
		*
		* @param $ids array The array of ids to delete.
		*
		* @return boolean Return true on successful deletion
		*/
		function multiDelete($ids=0)
		{
			if (!parent::multiDelete($ids)) {
				return false;
			}

			// Delete the assocaitions in the categoryassociations table
			$query = "DELETE FROM `".$this->tablePrefix."categoryassociations`
			WHERE `categoryid` IN (".(implode(',', array_keys($ids))).")";
			$this->db->Query($query);

			// Delete the assocaitions in the catassoc_history table
			$query = "DELETE FROM `".$this->tablePrefix."catassoc_history`
			WHERE `categoryid` IN (".(implode(',', array_keys($ids))).")";
			$this->db->Query($query);

			return true;
		}

		/**
		* CategoryExists
		* Check to see if a category with a given name exists under a given
		* parent categoryid
		*
		* @param int $parentid The id of the parent
		* @param int $name The name of the category
		*
		* @return boolean Does the category exist or not ?
		*/
		function CategoryExists($parentid, $name)
		{
			if (!$this->is_positive_int($parentid)) {
				return false;
			}

			$query = 'SELECT COUNT(*)
			FROM `'.$GLOBALS['tablePrefix'].'categories`
			WHERE `parentid`='.$parentid."
			AND `name`='".$GLOBALS['AKB_DB']->Quote($name)."'";

			$result = $GLOBALS['AKB_DB']->Query($query);

			$num = $GLOBALS['AKB_DB']->FetchOne($result);

			if ($num > 0) {
				return true;
			} else {
				return false;
			}
		}

		/**
		* validate_categoryid
		*
		* Ensure the categoryid is a pos int
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_categoryid($var)
		{
			return $this->is_positive_int($var);
		}

		/**
		* validate_name
		*
		* Ensure the name isn't empty or too long
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_name($var)
		{
			if (empty($var)) {
				$this->error = GetLang('apiCatNameEmpty');
				return false;
			}

			if (strlen($var) > 100) {
				$this->error = GetLang('apiCatNameLong');
				return false;
			}

			// Make sure a category cannot be renamed to have the same name
			// as an existing category at the same level
			if ($this->loaded) {
				if ($this->CategoryExists($this->parentid, $var)) {
					$this->error = GetLang('apiCatAlreadyExists');
					return false;
				}
			}

			return true;
		}

		/**
		* validate_description
		*
		* Ensure the description isn't too long
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_description($var)
		{
			if (strlen($var) > 255) {
				$this->error = GetLang('apiCatDescLong');
				return false;
			}

			return true;
		}

		/**
		* validate_parentid
		*
		* Ensure the parentid is a pos int
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_parentid($var)
		{
			return $this->is_positive_int($var);
		}

		/**
		* validate_approvaltype
		*
		* Ensure the approval type is between 0 and 2
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_approvaltype($var)
		{
			return ($var >= 0 and $var <= 2);
		}

		/**
		* validate_catorder
		*
		* Ensure the category order is a pos int
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_catorder($var)
		{
			return $this->is_positive_int($var);
		}
		
	}

?>
